<template>
    <div>
        <a-modal
            :title="modalTitle"
            v-model="modalShow"
            @ok="onSubmit"
            :width="1000"
            okText="确认"
            cancelText="取消"
        >
            <a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
                <a-row>
                    <a-col span="12">
                        <a-form-model-item label="物料编码">
                            <a-col span="20">
                                <a-input disabled v-model="form.materialCode" />
                            </a-col>
                            <a-col span="4">
                                <a-button type="primary" block icon="search" @click="onSearch" />
                            </a-col>
                        </a-form-model-item>
                    </a-col>
                    <a-col span="12">
                        <a-form-model-item label="物料名称">
                            <a-input disabled v-model="form.materialName" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col span="12">
                        <a-form-model-item label="批号">
                            <a-input disabled v-model="form.lotNo" />
                        </a-form-model-item>
                    </a-col>
                    <a-col span="12">
                        <a-form-model-item label="取样料量">
                            <a-input v-model="form.quantity" suffix="kg" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col span="12">
                        <a-form-model-item label="取样位置">
                            <a-input v-model="form.samplingSite" />
                        </a-form-model-item>
                    </a-col>
                    <a-col span="12">
                        <a-form-model-item label="取样去向">
                            <a-input v-model="form.purpose" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-form-model>
            <!-- 模态框 -->
            <material-query-modal ref="materialQueryModalRef" />
        </a-modal>
    </div>
</template>

<script>
import dict from "../../../dict.json";
import Bus from "../../../../utils/Bus";
import MaterialQueryModal from "../../../../commons/query/MaterialQueryModal";
export default {
    components: { MaterialQueryModal },
    data() {
        return {
            modalShow: false,
            codeDisabled: true,
            modalTitle: "",
            action: "",
            labelCol: { span: 7 },
            wrapperCol: { span: 16 },
            dict,
            form: {},
        };
    },
    methods: {
        // 打开窗口
        openModal(data) {
            this.form = !!data ? data : {};
            this.modalTitle = !data ? "新增样品" : "编辑样品";
            this.action = !data ? dict.action.sample.addAction : dict.action.sample.editAction;
            this.modalShow = true;
        },
        // 确定提交表单
        onSubmit() {
            let self = this;
            this.$store
                .dispatch(this.action, this.form)
                .then(res => {
                    if (parseInt(res.code) === 1) {
                        self.modalShow = false;
                        self.$message.success("成功");
                        self.$parent.init();
                    }
                })
                .catch(err => {
                    this.$message.error("失败" + res.msg + "(" + res.code + ")");
                    console.log(err);
                });
        },
        // 搜索
        onSearch() {
            this.$refs.materialQueryModalRef.openModal();
        },
    },
    mounted() {
        let self = this;
        Bus.$on("material", function (item) {
            // debugger;
            self.form.materialId = item.materialId;
            self.form.materialCode = item.materialCode;
            self.form.materialName = item.materialName;
            self.form.lotNo = item.materialLot;
            self.form.materialType = item.materialType;
            self.$forceUpdate();
        });
    },
};
</script>
